<?php
/**
 * Created by 深圳市丹宏昊天电子商务有限公司.
 * 作 者: sqingwen
 * 日 期: 2016/12/15
 *
 */
?>
<link rel="stylesheet" href="<?= base_url() . 'resource/layui/css/layui.css' ?>" media="all">
<link rel="stylesheet" type="text/css" href="<?= base_url() . 'resource/jeasyui/themes/metro/easyui.css' ?>"/>
<script type="text/javascript" src="<?= base_url() . 'resource/layui/layui.js' ?>" charset="utf-8"></script>
<script type="text/javascript" src="<?= base_url() . 'resource/js/layer/layer.js' ?>" charset="utf-8"></script>
<style>
 s{font-style: normal}
.layui-input-block{margin-left: 0px;margin-bottom: 10px;float: left;}
.layui-anim-upbit{float: left;width: 33%;}
.layui-form-checkbox{height: 21px;line-height: 19px;margin: 4px;}
.layui-anim-upbit dd{width: 90%;float: left;}
.layui-table th, .layui-table td{padding: 1px 10px;}
.layui-form-switch{position: relative;height: 20px;line-height: 20px;width: 40px;padding: 0px 5px;margin-top: 1px;border: 1px solid #d2d2d2;border-radius: 20px;transition: .1s linear;}
.btn_del_acc, .btn_del_fault{height: 21px;line-height: 21px;}
.bbb{display: none;}
a{color: #0a8e61;}

</style>
<script type="text/javascript">
    $(document).ready(function () {
        $("#customerId").combobox({
            onChange: function (n, o) {
                loadCustomizeAcc();
                loadCustomizeFault();
                loadCustomizeItem();
                loadIns();
            }
        });

    });
</script>

<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1><?=lang('menu_yhgl')?></h1>
          <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-pencil"></i>合作商维修项目</h3>
          </div>
          <div class="panel-body" style="padding: 15px;">
            <h3 class="panel-title" style="margin-bottom: 5px;">1 - 设置商家、分类及品牌</h3>
            <div class="well" style="padding:0 19px;">
              <div class="row">
                <div class="col-sm-3 layui-form-item">
                        <div class="form-group layui-input-block">
                            <label class="control-label">商家名称</label>
                             <div class="layui-input-block" style="width:100%;line-height: 27px;">
                                <select name="customerId" id="customerId" style="width:100%;" class="easyui-combobox form-control">
                                    <option value="-1">商家名称</option>
                                    <?php foreach ($customerList as $k => $v): ?>
                                        <option value="<?= $v['id'] ?>"><?= $v['name'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                     <div class="col-sm-3">
                        <div class="form-group">
                            <label class="control-label">选择产品</label>
                            <select class="form-control" name="categoryId" id="categoryId">
                            <option value="-1">选择产品</option>
                            <?php foreach ($categoryList as $k => $v): ?>
                                <option value="<?= $v['id'] ?>" dt-sapid="<?php
                                if (strripos($v['sap_id'] , '_') > 0) {
                                    echo substr($v['sap_id'] , 0 , strrpos($v['sap_id'] , '_'));
                                }
                                else {
                                    echo $v['sap_id'];
                                }
                                ?>"><?= $v['name_ch'] ?></option>
                            <?php endforeach; ?>
                        </select>
                        </div>
                    </div>
                     <div class="col-sm-3">
                        <div class="form-group">
                            <label class="control-label">选择品牌</label>
                            <select class="form-control" name="brandId" id="brandId">
                            <option value="-1">选择品牌</option>
                            <?php foreach ($brandList as $k => $v): ?>
                                <option class="bbb brbr_<?= $v['tid'] ?>" dt-tid="<?= $v['tid'] ?>"
                                        value="<?= $v['mbrandId'] ?>"
                                        dt-bid="<?= $v['bid'] ?>"><?= $v['name'] ?></option>
                            <?php endforeach; ?>
                        </select>
                        </div>
                    </div>
                     <div class="col-sm-3">
                        <div class="form-group">
                            <label class="control-label">选择型号</label>
                            <select class="form-control" name="productModel" id="productModel">
                                <option value="-1">请选择</option>
                            </select>
                        </div>
                    </div>
              </div>
            </div>

        <h3 class="panel-title" style="margin-bottom: 5px;">1 - 设置商家、分类及品牌</h3>
            <div class="layui-tab layui-tab-card " style="margin-top: 0px;">
                <ul class="layui-tab-title">
                    <li style="width:25%" class="layui-this">随机附件</li>
                    <li style="width:25%">故障项</li>
                    <li style="width:25%">维修项</li>
                    <li style="width:25%">检测费</li>

                </ul>
                <!-- 随机附件 begin -->
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show ">
                        <div style="height: 300px;overflow-y: auto;" class="div_acc">
                            <table class="layui-table" lay-skin="line">
                                <tr>
                                    <th>id</th>
                                    <th>产品类型</th>
                                    <th>附件中文</th>
                                    <th>附件英文</th>
                                    <th>添加时间</th>
                                    <th>添加人员</th>
                                    <th>选择</th>
                                </tr>
                                <?php foreach ($accessory as $row) { ?>
                                    <tr class="cct ccct_<?= $row['category'] ?>">
                                        <td><?= $row['id'] ?></td>
                                        <td><?= $row['typeName'] ?></td>
                                        <td><?= $row['name_ch'] ?></td>
                                        <td><?= $row['name_en'] ?></td>
                                        <td><?= date('Y-m-d' , $row['add_time']) ?></td>
                                        <td><?= $row['add_user'] ?></td>
                                        <td>
                                            <button class="layui-btn layui-btn-primary layui-btn-small cbk_acc"
                                                    dt-id="<?= $row['id'] ?>">
                                                <i class="layui-icon "
                                                   style="font-size: 24px; color: #1E9FFF; ">
                                                    &#xe61f;</i>
                                                添加
                                            </button>

                                        </td>
                                    </tr>
                                <?php } ?>

                            </table>
                        </div>
                        <!-- 这里处理 被选中 begin -->
                        <div style="clear:both;"></div>
                        <div id="show_selected" style="width:99%;min-height:180px;margin-top:10px;">
                            <fieldset class="layui-elem-field">
                                <legend id="show_title">已自定义的随机附件 （<em id="count_acc">0</em>）</legend>
                                <div class="layui-field-box layui-form"
                                     style="height: 200px;overflow-y: auto;">
                                    <table class="layui-table" lay-skin="line">
                                        <tr id="tb_acc">
                                            <th>id</th>
                                            <th>产品类型</th>
                                            <th>附件中文</th>
                                            <th>附件英文</th>
                                            <th>添加时间</th>
                                            <th>添加人员</th>
                                            <th>操作</th>
                                        </tr>
                                    </table>

                                </div>
                            </fieldset>

                        </div>
                        <!-- 这里处理 被选中 end -->

                    </div>
                    <!-- 随机附件 end -->

                    <!-- 故障项 begin -->
                    <div class="layui-tab-item">
                        <div style="height: 300px;overflow-y: auto;" class="div_fault">
                            <table class="layui-table" lay-skin="line">
                                <tr>
                                    <th>id</th>
                                    <th>产品类型</th>
                                    <th>故障中文</th>
                                    <th>故障英文</th>
                                    <th>添加时间</th>
                                    <th>添加人员</th>
                                    <th>选择</th>
                                </tr>
                                <?php foreach ($fault as $row) { ?>
                                    <tr class="cct ccct_<?= $row['category'] ?>">
                                        <td><?= $row['id'] ?></td>
                                        <td><?= $row['typeName'] ?></td>
                                        <td><?= $row['name_ch'] ?></td>
                                        <td><?= $row['name_en'] ?></td>
                                        <td><?= date('Y-m-d' , $row['add_time']) ?></td>
                                        <td><?= $row['add_user'] ?></td>
                                        <td>

                                            <button class="layui-btn layui-btn-primary layui-btn-small cbk_fault"
                                                    dt-id="<?= $row['id'] ?>">
                                                <i class="layui-icon "
                                                   style="font-size: 24px; color: #1E9FFF; ">
                                                    &#xe61f;</i>
                                                添加
                                            </button>


                                        </td>
                                    </tr>
                                <?php } ?>

                            </table>
                        </div>
                        <!-- 这里处理 被选中 begin -->
                        <div style="clear:both;"></div>
                        <div id="show_selected" style="width:99%;min-height:120px;margin-top:10px;">
                            <fieldset class="layui-elem-field">
                                <legend id="show_title">已自定义故障项（<em id="count_fault">0</em>）</legend>
                                <div class="layui-field-box layui-form"
                                     style="height: 200px;overflow-y: auto;">
                                    <table class="layui-table" lay-skin="line">
                                        <tr id="tb_fault">
                                            <th>id</th>
                                            <th>产品类型</th>
                                            <th>故障中文</th>
                                            <th>故障英文</th>
                                            <th>添加时间</th>
                                            <th>添加人员</th>
                                            <th>操作</th>
                                        </tr>
                                    </table>
                                </div>
                            </fieldset>

                        </div>
                        <!-- 这里处理 被选中 end -->
                    </div>
                    <!-- 故障项 end -->


                    <!-- 维修项 begin -->
                    <div class="layui-tab-item">
                        <div style="height: 300px;overflow-y: auto;" class="div_items">
                            <table class="layui-table" lay-skin="line">
                                <thead>
                                <tr>
                                    <th width="10%">id</th>
                                    <th width="10%">产品类型</th>
                                    <th width="10%">维修级别</th>
                                    <th width="10%">中文名称</th>
                                    <th width="10%">英文名称</th>
                                    <th width="10%">是否默认</th>
                                    <th width="10%">状态</th>
                                    <th width="10%">用户</th>
                                    <th width="10%">日期</th>
                                    <th width="10%">选择</th>
                                </tr>
                                </thead>

                                <tbody>
                                <?php
                                if ($ri):
                                    foreach ($ri as $key => $rs):
                                        ?>
                                        <tr class="cct ccct_<?= $rs['category'] ?>">
                                            <td width="10%"><?= $rs['id'] ?></td>
                                            <td width="10%"><?= $rs['categoryName'] ?></td>
                                            <td width="10%"><?= $rs['gradeName'] ?></td>
                                            <td width="10%"><?= $rs['name_ch'] ?></td>
                                            <td width="10%"><?= $rs['name_en'] ?></td>
                                            <td width="10%"><?= $rs['isDefault'] ?></td>
                                            <td width="10%"><?= $rs['availableName'] ?></td>
                                            <td width="10%"><?= $rs['add_user'] ?></td>
                                            <td width="10%"><?= $rs['add_time'] ?></td>
                                            <td width="10%" align="center">
                                                <button class="layui-btn layui-btn-primary layui-btn-small cbk_items"
                                                        dt-id="<?= $rs['id'] ?>">
                                                    <i class="layui-icon "
                                                       style="font-size: 24px; color: #1E9FFF; ">
                                                        &#xe61f;</i>
                                                    添加
                                                </button>


                                            </td>
                                        </tr>
                                        <?php
                                    endforeach;
                                endif;
                                ?>
                                </tbody>
                            </table>
                        </div>
                        <!-- 这里处理 被选中 begin -->
                        <div style="clear:both;"></div>
                        <div id="show_selected" style="width:99%;min-height:180px;margin-top:10px;">
                            <fieldset class="layui-elem-field">
                                <legend id="show_title">已选择维修项（<em id="count_item">0</em>）</legend>
                                <div class="layui-field-box layui-form"
                                     style="height: 200px;overflow-y: auto;">
                                    <table class="layui-table" lay-skin="line">
                                        <tr id="tb_item">
                                            <th>基础id</th>
                                            <th>产品类型</th>
                                            <th>维修级别</th>
                                            <th>中文名称</th>
                                            <th>英文名称</th>
                                            <th>品牌</th>
                                            <th>型号</th>
                                            <th>日期</th>
                                            <th style="display:none;">用户</th>
                                            <th>操作</th>
                                        </tr>
                                    </table>

                                </div>
                            </fieldset>

                        </div>
                        <!-- 这里处理 被选中 end -->
                    </div>
                    <!-- 维修项 end -->


                    <!-- 检测费 begin -->
                    <div class="layui-tab-item">
                        <table class="layui-table" lay-skin="line">
                            <tr style="line-height: 46px;height: 46px;">
                                <td style="width:20%; text-align: right">中文名称：</td>
                                <td><input type="text" name="txtNameCn" id="txtNameCn" value="检测费"/></td>
                            </tr>
                            <tr style="line-height: 46px;height: 46px;">
                                <td style="width:20%; text-align: right">英文名称：</td>
                                <td><input type="text" name="txtNameEn" id="txtNameEn"
                                           value="Testing costs"/></td>
                            </tr>
                            <tr style="line-height: 46px;height: 46px;">
                                <td style="width:20%; text-align: right">
                                    选项：
                                </td>
                                <td>
                                    商家：<label id="ins_merchant" style="font-weight: normal">NONE</label> /
                                    型号：<label id="ins_model" style="font-weight: normal">NONE</label>
                                </td>
                            </tr>
                            <tr style="line-height: 46px;height: 46px;">
                                <td style="width:20%; text-align: right">
                                    各站点费用：
                                </td>
                                <td>
                                    <?php foreach ($siteList as $k => $v) { ?>
                                        <span style="width:16%;float:left;"><?= $v['name_ch'] ?><br><input
                                                    type="text" class="ins_price_input"
                                                    onkeyup="value = value.replace(/[^\-?\d.]/g, '')"
                                                    name="price_<?= $v['id'] ?>" value="0.0"
                                                    style="width:80px"/></span>

                                    <?php } ?>
                                </td>
                            </tr>
                            <tr style="line-height: 46px;height: 46px;">
                                <td colspan="2" style="text-align: center;">
                                    <button class="layui-btn btnAddIns">添加</button>
                                    &nbsp;
                                </td>
                            </tr>
                        </table>


                        <!-- 这里处理 被选中 begin -->
                        <div style="clear:both;"></div>
                        <div id="show_selected" style="width:99%;min-height:180px;margin-top:10px;">
                            <fieldset class="layui-elem-field">
                                <legend id="show_title">已添加检测费（<em id="count_ins">0</em>）</legend>
                                <div class="layui-field-box layui-form"
                                     style="height: 200px;overflow-y: auto;">
                                    <table class="layui-table" lay-skin="line">
                                        <tr id="tb_ins">
                                            <th>id</th>
                                            <th>中文名称</th>
                                            <th>英文名称</th>
                                            <th>产品类型</th>
                                            <th>品牌</th>
                                            <th>型号</th>
                                            <th>费用</th>
                                            <th>操作</th>
                                        </tr>
                                    </table>

                                </div>
                            </fieldset>

                        </div>
                        <!-- 这里处理 被选中 end -->


                    </div>
                    <!-- 检测费 end   -->

                </div>
           
          </div>
        </div>
    </div>

<script>

    $('#brandId').change(function () {
    //alert($(this).find("option:selected").attr('dt-bid') + '---' + $(this).find("option:selected").attr('dt-tid'));
    $.ajax({
        type: "POST",
        url: '<?= blang_url('users/Partner/ajaxLoadModel') ?>',
        async: false,
        data: {
            'tid': $(this).find("option:selected").attr('dt-tid'),
            'bid': $(this).find("option:selected").attr('dt-bid')
        },
        dataType: "json",
        success: function (data) {

            //simc_modelId
            if (data.length > 0) {
                $str_sele_model = '<option value="-1">请选择</option>';
                for (var i = 0; i < data.length; i++) {

                    $str_sele_model = $str_sele_model + '<option value="' + data[i].id + '">' + data[i].name + '</option>';

                }
                $('#productModel').html($str_sele_model);
            }
        }
    });
});

$('#categoryId').change(function () {
    $str_sele_model = '<option value="-1">请选择</option>';
    $('#productModel').html($str_sele_model);
});
    
layui.use('form', function () {
        var form = layui.form();
        //随机附件begin ***********************************************************************
        $('.div_acc .cbk_acc').on('click', function () {
            var obj_acc = $(this).parents('tr:first');
            var val_acc = $(this).attr('dt-id');

            if (parseInt($("input[name='customerId']").val()) < 0) {

                layer.msg('亲，您没有选择商家哦！');
                $(this).removeClass('layui-form-checked');
                return;
            }
            if (parseInt($('#categoryId').val()) < 0) {
                layer.msg('亲，您没有选择产品类型哦！');
                $(this).removeClass('layui-form-checked');
                return;
            }
            /*if (parseInt($('#brandId').val()) < 0) {
             layer.msg('亲，您没有选择品牌哦！');
             $(this).removeClass('layui-form-checked');
             return;
             }*/

            // ajax添加自定义 begin
            $.ajax({
                type: "POST",
                url: '<?= blang_url('users/Partner/ajaxAddAccessory') ?>',
                async: false,
                data: {
                    'baseId': val_acc, //产品随机附件表id
                    'catId': $('#categoryId').val(), // 产品分类id
                    'brandId': $('#brandId').val(), // 品牌id
                    'merchant_id': $("input[name='customerId']").val(), //商家Id
                    'model_id': $('#productModel').val() //型号Id
                },
                beforeSend: function (XMLHttpRequest) {


                },
                complete: function (XMLHttpRequest, textStatus) {

                },
                dataType: "json",
                success: function (data) {
                    if (data == 1) {
                        layer.msg("恭喜亲，添加成功！");
                        loadCustomizeAcc();
                    }
                }
            });
            // ajax添加自定义 end


        });
        //随机附件end ***********************************************************************

        //故障 begin ***********************************************************************
        $('.div_fault .cbk_fault').on('click', function () {
            var obj_fault = $(this).parents('tr:first');
            var val_fault = $(this).attr("dt-id");


            if (parseInt($("input[name='customerId']").val()) < 0) {
                layer.msg('亲，您没有选择商家哦！');

                return;
            }
            if (parseInt($('#categoryId').val()) < 0) {
                layer.msg('亲，您没有选择产品类型哦！');

                return;
            }
            /*if (parseInt($('#brandId').val()) < 0) {
             layer.msg('亲，您没有选择品牌哦！');

             return;
             }*/

            // ajax添加自定义 begin
            $.ajax({
                type: "POST",
                url: '<?= blang_url('users/Partner/ajaxAddFault') ?>',
                async: false,
                data: {
                    'baseId': val_fault, //产品随机附件表id
                    'catId': $('#categoryId').val(), // 产品分类id
                    'brandId': $('#brandId').val(), // 品牌id
                    'merchant_id': $("input[name='customerId']").val(), //商家Id
                    'model_id': $('#productModel').val() //型号Id
                },
                beforeSend: function (XMLHttpRequest) {
                },
                complete: function (XMLHttpRequest, textStatus) {
                },
                dataType: "json",
                success: function (data) {
                    if (data == 1) {
                        layer.msg("恭喜亲，添加成功！");
                        loadCustomizeFault();
                    }
                }
            });
            // ajax添加自定义 end


        });
        //故障 end *************************************************************************


        //维修项 begin *********************************************************************
        $('.div_items .cbk_items').on('click', function () {
            var zindex = ($('.div_items .cbk_items').index($(this)));

            if (parseInt($("input[name='customerId']").val()) < 0) {
                layer.msg('亲，您没有选择商家哦！');
                $(this).removeClass('layui-form-checked');
                return;
            }
            if (parseInt($('#categoryId').val()) < 0) {
                layer.msg('亲，您没有选择产品类型哦！');
                $(this).removeClass('layui-form-checked');
                return;
            }
            /*if (parseInt($('#brandId').val()) < 0) {
             layer.msg('亲，您没有选择品牌哦！');
             $(this).removeClass('layui-form-checked');
             return;
             }*/

            // ajax添加自定义 begin
            $.ajax({
                type: "POST",
                url: '<?= blang_url('users/Partner/ajaxAddItem') ?>',
                async: false,
                data: {
                    'baseId': $('.cbk_items').eq(zindex).attr('dt-id'), //产品随机附件表id
                    'catId': $('#categoryId').val(), // 产品分类id
                    'brandId': $('#brandId').val(), // 品牌id
                    'merchant_id': $("input[name='customerId']").val(), //商家Id
                    'model_id': $('#productModel').val() //型号Id
                },
                beforeSend: function (XMLHttpRequest) {
                },
                complete: function (XMLHttpRequest, textStatus) {
                },
                dataType: "json",
                success: function (data) {
                    if (data == 1) {
                        layer.msg("恭喜亲，添加成功！");
                        loadCustomizeItem();
                    } else if (data == -1) {
                        layer.msg("抱歉，该类型已经存在同样的维修项，您可以指定品牌以及型号");
                    } else if (data == -2) {
                        layer.msg("抱歉，该型号已经存在同样的维修项");
                    } else if (data == -3) {
                        layer.msg("抱歉，该品牌、型号已经存在同样的维修项");
                    }
                }
            });
            // ajax添加自定义 end

        });
        //维修项 end ***********************************************************************
    });

    layui.use('element', function () {
        var element = layui.element();
    });


    //加载自定义随机附件 begin ********************************************************************************************
    loadCustomizeAcc = function () {
        if (parseInt($("input[name='customerId']").val()) < 1 || isNaN($("input[name='customerId']").val()) || $("input[name='customerId']").val() == '') {
            return false;
        }
        $('.tr_acc').remove();
        // ajax begin
        $.ajax({
            type: "POST",
            url: '<?= blang_url('users/Partner/ajaxLoadAccessory') ?>',
            async: false,
            data: {
                'merchant_id': $("input[name='customerId']").val() //商家Id
            },
            dataType: "json",
            success: function (data) {


                //开始加载
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        str_tr_acc = '<tr class="tr_acc">' +
                            '<td>' + data[i].id + '</td>' +
                            '<td>' + data[i].cat_name + '</td>' +
                            '<td><input type="text" dt-id="' + data[i].id + '" dt-type="name_ch" value="' + data[i].name_ch + '" dt-value="' + data[i].name_ch + '"/></td>' +
                            '<td><input type="text" dt-id="' + data[i].id + '" dt-type="name_en"  value="' + data[i].name_en + '" dt-value="' + data[i].name_en + '"/></td>' +
                            '<td>' + data[i].add_time + '</td>' +
                            '<td>' + data[i].add_user + '</td>' +
                            '<td><button class="layui-btn btn_del_acc" dt-id="' + data[i].id + '">删除</button></td>' +
                            '</tr>';
                        $('#tb_acc').after(str_tr_acc);
                        $('#count_acc').text($('.tr_acc').length);
                    }
                } else {

                    $('#tb_acc').after('<tr class="tr_acc"><td colspan="7"><center>暂时没有数据</center></td></tr>');
                    $('#count_acc').text('0');
                }


            }
        });
        // ajax end

        //设置ajax修改名称事件 begin

        $('.tr_acc input').blur(function () {
            //如果值相同则不修改
            if ($(this).val() == $(this).attr('dt-value')) {
                return;
            }
            $.ajax({
                type: "POST",
                url: '<?= blang_url('users/Partner/ajaxMdyCustomizeAccessory') ?>',
                async: false,
                data: {
                    'id': $(this).attr('dt-id'),
                    'filed': $(this).attr('dt-type'),
                    'val': $(this).val()
                },
                dataType: "json",
                success: function (data) {
                    if (data == 1) {
                        layer.msg("修改成功");
                        loadCustomizeAcc();
                    }
                }
            });

        });

        //设置ajax修改名称事件 begin

        //设置ajax删除事件 begin  

        $('.btn_del_acc').click(function () {
            var del_acc_id = $(this).attr('dt-id');
            layer.confirm('您确定要删除么？', {
                btn: ['删除', '保留'], //按钮
                shade: false //不显示遮罩
            }, function () {
                $.ajax({
                    type: "POST",
                    url: '<?= blang_url('users/Partner/ajaxDelCustomizeAccessory') ?>',
                    async: false,
                    data: {
                        'id': del_acc_id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data == 1) {
                            layer.msg("删除成功");
                            loadCustomizeAcc();
                        }
                    }
                });

            }, function () {

            });

        });

        //设置ajax删除名称事件 begin
    }
    //加载自定义随机附件 end**********************************************************************************************


    //加载自定义故障 begin ********************************************************************************************
    loadCustomizeFault = function () {
        if (parseInt($("input[name='customerId']").val()) < 1 || isNaN($("input[name='customerId']").val()) || $("input[name='customerId']").val() == '') {
            return false;
        }
        $('.tr_fault').remove();
        // ajax begin
        $.ajax({
            type: "POST",
            url: '<?= blang_url('users/Partner/ajaxLoadFault') ?>',
            async: false,
            data: {
                'merchant_id': $("input[name='customerId']").val() //商家Id
            },
            dataType: "json",
            success: function (data) {


                //开始加载
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        str_tr_fault = '<tr class="tr_fault">' +
                            '<td>' + data[i].id + '</td>' +
                            '<td>' + data[i].cat_name + '</td>' +
                            '<td><input type="text" dt-id="' + data[i].id + '" dt-type="name_ch" value="' + data[i].name_ch + '" dt-value="' + data[i].name_ch + '"/></td>' +
                            '<td><input type="text" dt-id="' + data[i].id + '" dt-type="name_en"  value="' + data[i].name_en + '" dt-value="' + data[i].name_en + '"/></td>' +
                            '<td>' + data[i].add_time + '</td>' +
                            '<td>' + data[i].add_user + '</td>' +
                            '<td><button class="layui-btn btn_del_fault" dt-id="' + data[i].id + '">删除</button></td>' +
                            '</tr>';
                        $('#tb_fault').after(str_tr_fault);
                        $('#count_fault').text($('.tr_fault').length);
                    }
                } else {

                    $('#tb_fault').after('<tr class="tr_fault"><td colspan="7"><center>暂时没有数据</center></td></tr>');
                    $('#count_fault').text('0');
                }


            }
        });
        // ajax end

        //设置ajax修改名称事件 begin

        $('.tr_fault input').blur(function () {
            //如果值相同则不修改
            if ($(this).val() == $(this).attr('dt-value')) {
                return;
            }
            $.ajax({
                type: "POST",
                url: '<?= blang_url('users/Partner/ajaxMdyCustomizeFault') ?>',
                async: false,
                data: {
                    'id': $(this).attr('dt-id'),
                    'filed': $(this).attr('dt-type'),
                    'val': $(this).val()
                },
                dataType: "json",
                success: function (data) {
                    if (data == 1) {
                        layer.msg("修改成功");
                        loadCustomizeFault();
                    }
                }
            });

        });

        //设置ajax修改名称事件 begin

        //设置ajax删除事件 begin


        $('.btn_del_fault').click(function () {
            var del_fault_id = $(this).attr('dt-id');
            layer.confirm('您确定要删除么？', {
                btn: ['删除', '保留'], //按钮
                shade: false //不显示遮罩
            }, function () {
                $.ajax({
                    type: "POST",
                    url: '<?= blang_url('users/Partner/ajaxDelCustomizeFault') ?>',
                    async: false,
                    data: {
                        'id': del_fault_id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data == 1) {
                            layer.msg("删除成功");
                            loadCustomizeFault();
                        }
                    }
                });

            }, function () {

            });

        });

        //设置ajax删除名称事件 begin
    }
    //加载自定义故障 end**********************************************************************************************


    //加载自定义故障 begin ********************************************************************************************
    loadCustomizeItem = function () {
        if (parseInt($("input[name='customerId']").val()) < 1 || isNaN($("input[name='customerId']").val()) || $("input[name='customerId']").val() == '') {
            return false;
        }
        $('.tr_item').remove();
        $.ajax({
            type: "POST",
            url: '<?= blang_url('users/Partner/ajaxLoadItem') ?>',
            async: false,
            data: {
                'merchant_id': $("input[name='customerId']").val() //商家Id
            },
            dataType: "json",
            success: function (data) {
                // data = JsonSort(data, 'id');
                //开始加载
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        str_class = '';
                        str_tips = '';
                        if (data[i].is_sync == 1)
                        {
                            str_class = ' style="color:darkgreen" ';
                            str_tips = ' title="基础维修项同步数据" ';
                        }
                        str_tr_item = '<tr  class="tr_item" ' + str_tips + str_class + ' id="item_id_' + data[i].id + '">' +
                                '<td>#' + data[i].labor_id + '</td>' +
                                '<td>' + data[i].cat_name + '</td>' +
                                '<td>' + data[i].item_level + '</td>' +
                                '<td>' + data[i].name_ch + '</td>' +
                                '<td>' + data[i].name_en + '</td>' +
                                '<td>' + data[i].brand_name + '</td>' +
                                '<td>' + data[i].model_name + '</td>' +
                                '<td>' + data[i].add_time + '</td>' +
                                '<td style="display:none;">' + data[i].add_user + '</td>' +
                                '<td><button class="layui-btn btn_mdy_item" dt-id="' + data[i].id + '"  dt-baseid="' + data[i].labor_id + '" dt-catid="' + data[i].category_id + '" dt-cusid="' + data[i].customer_id + '" dt-brandid="' + data[i].brand_id + '">编辑</button>&nbsp;<button class="layui-btn btn_del_item" dt-id="' + data[i].id + '"  dt-baseid="' + data[i].labor_id + '" dt-catid="' + data[i].category_id + '" dt-cusid="' + data[i].customer_id + '" dt-brandid="' + data[i].brand_id + '">删除</button></td>' +
                                '</tr>';
                        $('#tb_item').after(str_tr_item);
                        $('#count_item').text($('.tr_item').length);
                    }
                } else {
                    $('#tb_item').after('<tr class="tr_item"><td colspan="7"><center>暂时没有数据</center></td></tr>');
                    $('#count_item').text('0');
                }


            }
        });

        //设置ajax删除事件 begin


        $('.btn_del_item').click(function () {

            var bdi_laborid = $(this).attr('dt-baseid');
            var bdi_catid = $(this).attr('dt-catid');
            var bdi_brandid = $(this).attr('dt-brandid');
            var bdi_cusid = $(this).attr('dt-cusid');
            layer.confirm('您确定要删除么？', {
                btn: ['删除', '保留'], //按钮
                shade: false //不显示遮罩
            }, function () {
                $.ajax({
                    type: "POST",
                    url: '<?= blang_url('users/Partner/ajaxDelCustomizeItem') ?>',
                    async: false,
                    data: {
                        'laborid': bdi_laborid,
                        'catid': bdi_catid,
                        'brandid': bdi_brandid,
                        'cusid': bdi_cusid
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data == 1) {
                            layer.msg("删除成功");
                            loadCustomizeItem();
                        }
                    }
                });

            }, function () {

            });


        });
        //设置ajax删除名称事件 begin


        //ajax 修改的函数 begin
        $('.btn_mdy_item').click(function () {
            var mdy_item_id = $(this).attr("dt-id");
            layer.open({
                type: 1,
                title: "修改维修项",
                skin: 'layui-layer-rim', //加上边框
                area: ['420px', '370px'], //宽高
                content: loadMdyPanel($(this).attr("dt-baseid"), $(this).attr("dt-cusid"))
            });
            $('.txt_price').keyup(function () {
                this.value = this.value.replace(/[^\d.]/g, '');
            });
            $('#txtItemNameCn').val($('#item_id_' + mdy_item_id).find('td').eq(3).text());//中文名
            $('#txtItemNameEn').val($('#item_id_' + mdy_item_id).find('td').eq(4).text());//英文名
            $('#simc_categoryId option').each(function () {
                if ($(this).text() == $('#item_id_' + mdy_item_id).find('td').eq(1).text()) {
                    $(this).attr("selected", "selected");
                }
            });

            $('#simc_gradeId option').each(function () {
                if ($(this).text() == $('#item_id_' + mdy_item_id).find('td').eq(2).text()) {
                    $(this).attr("selected", "selected");
                }
            });
            $('#simc_brandId option').each(function () {
                if ($(this).text() == $('#item_id_' + mdy_item_id).find('td').eq(5).text()) {
                    $(this).attr("selected", "selected");
                }
            });




            $.ajax({
                type: "POST",
                url: '<?= blang_url('users/Partner/ajaxLoadModel') ?>',
                async: false,
                data: {
                    'tid': $('#simc_categoryId').find("option:selected").attr("dt-sapid"),
                    'bid': $('#simc_brandId').find("option:selected").attr("dt-sapid")
                },
                dataType: "json",
                success: function (data) {

                    //simc_modelId
                    if (data.length > 0) {
                        $str_sele_model = '<option value="-1">请选择</option>';
                        for (var i = 0; i < data.length; i++) {

                            $str_sele_model = $str_sele_model + '<option value="' + data[i].id + '">' + data[i].name + '</option>';

                        }
                        $('#simc_modelId').html($str_sele_model);
                    }

                }
            });

            $('#simc_modelId option').each(function () {
                if ($(this).text() == $('#item_id_' + mdy_item_id).find('td').eq(6).text()) {
                    $(this).attr("selected", "selected");
                }
            });

            loadSitePrice($(this).attr("dt-baseid"), $(this).attr("dt-cusid"));
            /*
             $('#simc_categoryId option').each(function(){
             if($(this).text()==$('#item_id_'+mdy_item_id).find('td').eq(1).text())
             {
             $(this).attr("selected","selected");
             }
             });*/


        });
        //ajax 修改的函数 end
    }

    //加载自定义维修项 begin --------------------------------------------------------------
    loadSitePrice = function (laborId, merId) {
        $.ajax({
            type: "POST",
            url: '<?= blang_url('users/Partner/ajaxGetCustomerPrice') ?>',
            async: false,
            data: {
                'lid': laborId,
                'mid': merId
            },
            dataType: "json",
            success: function (data) {
                //console.log(data);
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        $('#price_' + data[i].site_id).val(data[i].price);
                    }
                }
            }
        });
    }
    //加载自定义维修项   end --------------------------------------------------------------

    loadMdyPanel = function (baseId, merid) {


        var str_item_mdy_content = '<div style="width:96%;margin:0 auto;"><table class="layui-table" lay-skin="line">' +
            '<tr><td>中文名</td><td><input type="text" id="txtItemNameCn" name="txtItemNameCn"/></td></tr>' +
            '<tr><td>英文名</td><td><input type="text" id="txtItemNameEn" name="txtItemNameEn"/></td></tr>' +
            '<tr><td>维修级别</td><td><select name="simc_gradeId" id="simc_gradeId" ><?php foreach ($gradeList as $k => $v): ?><option value="<?= $v['id'] ?>"><?= $v['name_ch'] ?></option><?php endforeach; ?></select></td></tr>' +
            '<tr><td>产品类型</td><td><select name="simc_categoryId" id="simc_categoryId" disabled="disabled"><?php foreach ($categoryList as $k => $v): ?><option  dt-sapid="<?php
                if (strripos($v['sap_id'] , '_') > 0) {
                    echo substr($v['sap_id'] , 0 , strrpos($v['sap_id'] , '_'));
                }
                else {
                    echo $v['sap_id'];
                }
                ?>" value="<?= $v['id'] ?>"><?= $v['name_ch'] ?></option><?php endforeach; ?></select></td></tr>' +
            '<tr><td>产品品牌</td><td><select name="simc_brandId" id="simc_brandId" disabled="disabled"><option value="-1">请选择</option><?php foreach ($brandList as $k => $v): ?><option value="<?= $v['mbrandId'] ?>" dt-sapid="<?= $v['sap_id'] ?>"><?= $v['name'] ?></option><?php endforeach; ?></select></td></tr>' +
            '<tr><td>产品型号</td><td><select name="simc_modelId" id="simc_modelId" ><option value="-1">请选择</option></select></td></tr>' +
            '<tr><td colspan="2"><center><strong>基本费用</strong></center></td></tr>' +
            '<tr><td colspan="2"><?php foreach ($siteList as $k => $v): ?><li style="width:30%;float:left;line-height:25px;"><label style="width:40px;float:left;text-align:right;"><?= $v['name_ch'] ?></label>:<input type="text" value="" id="price_<?= $v['id'] ?>" dt-siteid="<?= $v['id'] ?>" style="width:50px;" class="txt_price"/></li><?php endforeach; ?></td></tr>' +
            '<tr><td colspan="2" align="center"><button class="layui-btn btn-item-mdy" onclick="mdyItem(' + baseId + ',' + merid + ')" >修改</button></td></tr>' +
            '</table></div>';

        return str_item_mdy_content;
    }


    //修改维修项 begin

    mdyItem = function (baseId, merid) {

        var item_mdy_cnname = $.trim($('#txtItemNameCn').val());//中文名
        var item_mdy_enname = $.trim($('#txtItemNameEn').val());//英文名
        var simc_gradeId = $('#simc_gradeId').val();//维修等级
        var simc_categoryId = $('#simc_categoryId').val();//维修类型
        var simc_brandId = $('#simc_brandId').val();//维修类型
        var simc_modelId = $('#simc_modelId').val();//维修产品型号
        var prices = '';
        var sites = '';
        $('.txt_price').each(function () {
            if ($('.txt_price').index($(this)) == 0) {
                prices = $(this).val();
                sites = $(this).attr('dt-siteid');
            } else {
                prices = prices + ',' + ($(this).val() == '' ? '0' : $(this).val());
                sites = sites + ',' + $(this).attr('dt-siteid');
            }
        })

        $.ajax({
            type: "POST",
            url: '<?= blang_url('users/Partner/ajaxMdyItem') ?>',
            async: false,
            dataType: "text",
            data: {
                'cnname': item_mdy_cnname,
                'enname': item_mdy_enname,
                'category': simc_categoryId,
                'grade': simc_gradeId,
                'price': prices,
                'laborId': baseId,
                'cusId': merid,
                'siteIds': sites,
                'brandId': simc_brandId,
                'modelId': simc_modelId

            },
            success: function (data) {
                console.log(data);
                if (data > 0) {
                    layer.msg("恭喜你，修改成功！");
                    layer.closeAll();
                    loadCustomizeItem();
                }
            }
        });
        //console.log(prices);

    }
    //修改维修项 end

    //加载自定义故障 end   ********************************************************************************************
    function JsonSort(json, key) {
        //console.log(json);
        for (var j = 1, jl = json.length; j < jl; j++) {
            var temp = json[j],
                val = temp[key],
                i = j - 1;
            while (i >= 0 && json[i][key] > val) {
                json[i + 1] = json[i];
                i = i - 1;
            }
            json[i + 1] = temp;

        }
        //console.log(json);
        return json;
    }


    $('#categoryId').change(function () {
        $('.cct').hide();
        $('.ccct_' + $(this).val()).show();

        $('.bbb').hide();
        var dt_sapid = $(this).find("option:selected").attr("dt-sapid");
        $('.brbr_' + dt_sapid).show();


    });

    $('#brandId').change(function () {
        var bb_id = $(this).find("option:selected").attr("dt-bid");
        $('.bbrand').hide();
        $('.bbbbarnd_' + bb_id).show();
    });

    //设置检测费的模块
    $('#productModel').change(function () {
        $('#ins_model').text($(this).find("option:selected").text());
        //清除所有的选中状态
        $('.layui-form-checkbox').removeClass('layui-form-checked');
        $('checkbox').removeAttr("checked");
    });
    //设置检测费的模块

    $(document).ready(function () {
        $("#customerId").combobox({
            onChange: function (n, o) {
                //n是value值，o是上一次值
                $('#ins_merchant').text($('#customerId').combobox('getText'));
                loadCustomizeAcc();
                loadCustomizeFault();
                loadCustomizeItem();
                loadIns();

            }
        });
    });


    $('.btnAddIns').click(function () {
        var mer = $.trim($('#ins_merchant').text());
        var modl = $.trim($('#ins_model').text());
        var insss = ($('.ins_price_input').serialize());

        if (mer == 'NONE') {
            layer.msg("商家不能为空！");
            return;
        }
        var cnt_price_stat = 0;
        $('.ins_price_input').each(function () {
            if (parseInt($(this).val()) > 0) {
                cnt_price_stat = cnt_price_stat + 1;
            }

        });
        if (cnt_price_stat == 0) {
            layer.msg("请至少填写一个价格！");
            return;
        }


        //如果都没有问题就提交数据了

        $.ajax({
            type: "POST",
            url: '<?= blang_url('users/Partner/ajaxAddTestingPrice') ?>',
            async: false,
            data: {
                'name_cn': $('#txtNameCn').val(), //品牌名称
                'name_en': $('#txtNameEn').val(), // 品牌名称
                'category_id': $('#categoryId').val(), // 产品类型id
                'brand_id': $('#brandId').val(), //品牌id
                'merchant_id': $('#customerId').combobox('getValue'), //商家id
                'model_id': $('#productModel').val(),
                'price_arr': insss//价格体系
            },
            beforeSend: function (XMLHttpRequest) {


            },
            complete: function (XMLHttpRequest, textStatus) {

            },
            dataType: "json",
            success: function (data) {
                if (data) {
                    layer.msg("恭喜亲，添加成功！");
                    $('.tr_ins').remove();
                    $('#count_ins').text(data.length);
                    loadIns();

                }
            }
        });

    });

    loadIns = function () {

        if (parseInt($("input[name='customerId']").val()) < 1 || isNaN($("input[name='customerId']").val()) || $("input[name='customerId']").val() == '') {
            return false;
        }

        $('.tr_ins').remove();
        $.ajax({
            type: "POST",
            url: '<?= blang_url('users/Partner/ajaxGetPrice') ?>',
            async: false,
            data: {

                'merchant_id': $('#customerId').combobox('getValue')
            },
            beforeSend: function (XMLHttpRequest) {


            },
            complete: function (XMLHttpRequest, textStatus) {

            },
            dataType: "json",
            success: function (data) {
                if (data) {
                    $('#count_ins').text(data.length);
                    for (var i = 0; i < data.length; i++) {

                        str_priceee = '';
                        for (var ii = 0; ii < data[i].pricelist.length; ii++) {
                            str_priceee = str_priceee + '[' + data[i].pricelist[ii].site_name + ':' + data[i].pricelist[ii].merchant_price + ']';
                        }
                        ttd = '<tr class="tr_ins" style="height:32px;">' +
                            '<td>' + data[i].id + '</td>' +
                            '<td>' + data[i].name_cn + '</td>' +
                            '<td>' + data[i].name_en + '</td>' +
                            '<td>' + data[i].category + '</td>' +
                            '<td>' + data[i].brand + '</td>' +
                            '<td>' + data[i].model + '</td>' +
                            '<td>' + str_priceee + '</td>' +
                            '<td><button class="layui-btn-primary btnInsDel layui-btn-small" dt-id="' + data[i].id + '">删除</button></td>' +
                            '</tr>';
                        $('#tb_ins').after(ttd);
                    }
                    $('.btnInsDel').click(function () {
                        $.ajax({
                            type: "POST",
                            url: '<?= blang_url('users/Partner/ajaxDelPrice') ?>',
                            async: false,
                            data: {
                                'id': $(this).attr('dt-id')
                            },
                            dataType: "text",
                            success: function (data) {
                                if (data) {
                                    layer.msg("删除成功！");
                                    loadIns();
                                }

                            }
                        });

                    });

                }
            }
        });
    }

</script>

</div>
